package com.chinda.modules.job.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.chinda.common.annotation.SysLog;
import com.chinda.common.enums.MessageEnum;
import com.chinda.common.exception.RRException;
import com.chinda.common.utils.R;
import com.chinda.modules.job.dto.ScheduleJobLogDTO;
import com.chinda.modules.job.entity.ScheduleJobLogEntity;
import com.chinda.modules.job.qo.ScheduleJobLogQO;
import com.chinda.modules.job.service.ScheduleJobLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;

/**
 * 定时任务日志
 *
 * @author chinda
 * @date 2021-07-09 19:09:10
 */
@AllArgsConstructor
@RestController
@RequestMapping("/schedulejoblogs")
@Api(tags = "定时任务日志")
public class ScheduleJobLogController {

    private final ScheduleJobLogService scheduleJobLogService;

    /**
     * 分页
     *
     * @param qo
     * @return
     */
    @SysLog("查询定时任务日志分页")
    @PostMapping("/page")
    @ApiOperation(value = "查询分页信息")
    public R page(@ApiParam(value = "qo", name = "查询实例", required = true) @RequestBody ScheduleJobLogQO qo) {
        return R.ok().set(scheduleJobLogService.page(qo));
    }

    /**
     * 获取详情
     *
     * @return
     */
    @SysLog("查询定时任务日志详情")
    @GetMapping("/{logId}")
    @ApiOperation(value = "获取详情" )
    public R info(@PathVariable("logId")  Long logId) {
        ScheduleJobLogEntity scheduleJobLog = scheduleJobLogService.getById(logId);
        return R.ok().set(scheduleJobLog);
    }

    /**
     * 保存
     *
     * @return
     */
    @SysLog("保存定时任务日志")
    @PostMapping
    @ApiOperation(value = "保存信息")
    public R save(@ApiParam(value = "scheduleJobLogDTO", name = "定时任务日志DTO实例", required = true) @RequestBody @Valid ScheduleJobLogDTO scheduleJobLogDTO) {
        ScheduleJobLogEntity scheduleJobLog = new ScheduleJobLogEntity();
        BeanUtil.copyProperties(scheduleJobLogDTO, scheduleJobLog, CopyOptions.create().ignoreNullValue());
        scheduleJobLogService.save(scheduleJobLog);
        return R.ok().set(scheduleJobLog);
    }

    /**
     * 修改全属性
     *
     * @param scheduleJobLogDTO
     * @return
     */
    @SysLog("修改定时任务日志全属性")
    @PutMapping("/{logId}")
    @ApiOperation(value = "修改全属性")
    public R modify(@PathVariable("logId") Long logId,
                    @ApiParam(value = "scheduleJobLogDTO", name = "定时任务日志DTO实例", required = true) @RequestBody @Valid ScheduleJobLogDTO scheduleJobLogDTO) {
        ScheduleJobLogEntity scheduleJobLog = new ScheduleJobLogEntity();
        BeanUtil.copyProperties(scheduleJobLogDTO, scheduleJobLog, CopyOptions.create().ignoreNullValue());
        scheduleJobLog.setLogId(logId);
        if (!scheduleJobLogService.updateById(scheduleJobLog)) {
            throw new RRException(MessageEnum.SYS_CHECK_0012.getMsg());
        }
        return R.ok().set(scheduleJobLog);
    }

    /**
     * 修改局部属性
     *
     * @param logId
     * @param scheduleJobLogDTO
     * @return
     */
    @SysLog("修改定时任务日志局部属性")
    @PatchMapping("/{logId}")
    @ApiOperation(value = "修改局部属性")
    public R patch(@PathVariable("logId") Long logId,
                   @ApiParam(value = "scheduleJobLogDTO", name = "定时任务日志DTO实例", required = true) @RequestBody @Valid ScheduleJobLogDTO scheduleJobLogDTO) {
        ScheduleJobLogEntity dbScheduleJobLog = scheduleJobLogService.getById(logId);
        if (dbScheduleJobLog == null) {
            throw new RRException(MessageEnum.SYS_CHECK_0022.getMsg());
        }
        BeanUtil.copyProperties(scheduleJobLogDTO, dbScheduleJobLog, CopyOptions.create().ignoreNullValue());
        if (!scheduleJobLogService.updateById(dbScheduleJobLog)) {
            throw new RRException(MessageEnum.SYS_CHECK_0012.getMsg());
        }
        return R.ok().set(dbScheduleJobLog);
    }

    /**
     * 删除数据
     *
     * @param logId
     * @return
     */
    @SysLog("删除定时任务日志")
    @DeleteMapping("/{logId}")
    @ApiOperation(value = "删除数据")
    public R remove(@PathVariable("logId") Long logId) {
        scheduleJobLogService.removeById(logId);
        return R.ok();
    }
    /**
     * 批量删除数据
     *
     * @param logIds
     * @return
     */
    @SysLog("批量删除定时任务日志")
    @DeleteMapping("/batch")
    @ApiOperation(value = "批量删除数据")
    public R remove(@ApiParam(value = "logIds", name = "logId集合", required = true) @RequestBody Long [] logIds) {
        scheduleJobLogService.removeByIds(Arrays.asList(logIds));
        return R.ok();
    }
}
